﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Abp.Events.Bus;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Castle.MicroKernel.Registration;
using GCT.Foundation.Configuration;
using Microsoft.Extensions.Configuration;

namespace GCT.Foundation
{
    [DependsOn(
        typeof(FoundationTemplateEntityFrameworkCoreModule)
        )]
    public class FoundationTemplateMigratorModule : AbpModule
    {
        private readonly IConfigurationRoot _appConfiguration;

        public FoundationTemplateMigratorModule()
        {
            FoundationTemplateConfigs.Database.SkipDbSeed = true;

            _appConfiguration = AppConfigurations.Get(
                typeof(FoundationTemplateMigratorModule).GetAssembly().GetDirectoryPathOrNull()
            );
        }

        public override void PreInitialize()
        {
            Configuration.DefaultNameOrConnectionString = _appConfiguration.ConnectionStringsDefault();

            Configuration.BackgroundJobs.IsJobExecutionEnabled = false;

            Configuration.ReplaceService(
                typeof(IEventBus),
                () => IocManager.IocContainer.Register(
                    Component.For<IEventBus>().Instance(NullEventBus.Instance)
                )
            );
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(
                typeof(FoundationTemplateMigratorModule).GetAssembly()
            );
        }
    }
}
